package com.dzq.utils
{
	import flash.display.Graphics;
	
	/**
	 * @author Ethan.Du
	 * @date 2008-05-19
	 */
public class GraphicsUtil
{
			public static var dotLength:Number = 10;
            public static var gap:Number = 4;
            public static var start:Number = 0;
            public static var boderThick:int = 1;
            public static var borderColor:uint = 0x3e3e3e;
            
            /**
			 * Draw Dotted Circle
			 * @param g the graphics context to draw with
			 * @param x the center x-coordinate of the arc
			 * @param y the center y-coorindate of the arc
			 * @param radius the radius of the arc
			 * 
			 */
			public static function drawDottedCircle(g:Graphics, x:Number, y:Number, 
										radius:Number, dotLength:Number, gap:Number):void{
				for(var i:int =0; i<= radius * Math.PI * 2; i+= dotLength + gap ){
					
					drawArc(g, x, y, radius, i/radius , (i + dotLength)/radius );
				}							
			}
			/**
			 * Draws an arc (a segment of a circle's circumference)
			 * @param g the graphics context to draw with
			 * @param x the center x-coordinate of the arc
			 * @param y the center y-coorindate of the arc
			 * @param radius the radius of the arc
			 * @param a0 the starting angle of the arc (in radians)
			 * @param a1 the ending angle of the arc (in radians)
			 */
			public static function drawArc(g:Graphics, x:Number, y:Number, 
										radius:Number, a0:Number, a1:Number) : void
			{
				var slices:Number = (Math.abs(a1-a0) * radius) / 4;
				var a:Number, cx:Number = x, cy:Number = y;
				
				for (var i:uint = 0; i <= slices; ++i) {
					a = a0 + i*(a1-a0)/slices;
					x = cx + radius * Math.cos(a);
					y = cy + -radius * Math.sin(a);
					if (i==0) {
						g.moveTo(x, y);
					} else {
						g.lineTo(x,y);
					}
				}
			}
            public static function drawDottenRect(g:Graphics, x0:Number, y0:Number,
            												  x1:Number, y1:Number):void{
            	
            	g.clear();
            	g.lineStyle(boderThick, borderColor);
            	drawDottedHorizontalLineTo(g,y0,x0,x1);
            	drawDottedVerticalLineTo(g,x0,y0,y1);
            	drawDottedHorizontalLineTo(g,y1,x1,x0);
            	drawDottedVerticalLineTo(g,x1,y0,y1);         				
			}
			
			
			public static function drawDottedHorizontalLineTo( g:Graphics, y:Number, 
																		   x0:Number, x1:Number):void
			{
				if( x0 < x1){
					g.moveTo(x0, y);
					for(var x:Number = x0 + start; x<x1; x+= dotLength + gap){
						if( x + dotLength + gap <= x1){
							g.lineTo(x + dotLength,y);
							g.moveTo(x + dotLength + gap, y);
						}else{
							if(x + dotLength > x1){
								g.lineTo(x1, y);
							}else{
								g.lineTo(x + dotLength,y);
								
							}
						}
						
					}
				}else{
					g.moveTo(x0, y);
					for(var x:Number = x0  ; x>x1; x-= dotLength + gap){
						
						if(x - (dotLength + gap )>=x1){
							g.lineTo(x - dotLength,y);
							g.moveTo(x - dotLength - gap, y);
						}else{
							if(x - dotLength < x1){
								g.lineTo(x1, y);
							}else{
								g.lineTo( x - dotLength, y);
							}
						}
						
					}
				}
				
			}		
			
			public static function drawDottedVerticalLineTo(g:Graphics, x :Number, 
																		y0 :Number, y1 :Number ):void
			{
				if(y0 <= y1){
					g.moveTo(x, y0);
					for(var y:Number=y0; y <= y1; y+= dotLength + gap){
						if( y + dotLength + gap <= y1){
							g.lineTo(x, y + dotLength);
							g.moveTo(x, y + dotLength + gap);
						}else{
							if( y + dotLength > y1){
								g.lineTo(x, y1);
							}else{
								g.lineTo( x, y + dotLength );
							}
						}
						
					}
				}else{
					g.moveTo(x, y1);
					for(var y:Number = y0; y>= y1; y-=dotLength + gap){
						if(y - dotLength  - gap >= y1){
							g.lineTo(x, y - dotLength);
							g.moveTo(x, y - dotLength - gap);
						}else{
							if(y - dotLength < y1){
								g.lineTo(x, y1);
							}else{
								g.lineTo(x, y - dotLength );
							}
						}
						
					}
				}
			}


}
}